<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/9/22
 * Time: 14:26
 */
namespace App\Service\Admin;

use App\Http\Model\Mysql\CategoryModel;
use Carbon\Carbon;

class CategoryService extends BaseService
{
    /**
     * 获取所有分类
     * @return array
     */
    public function catList()
    {
        //查看所有分类
        $all_cat    =   CategoryModel::where(['is_delete' => CategoryModel::NOT_DELETE])->select(['id', 'parent_id', 'cat_name', 'level', 'cat_icon', 'sorting', 'created_at', 'updated_at'])
                        ->orderBy('sorting', 'desc')->get()->each(function (&$item) {
                            $item->create_time   =   $item->created_at->toDateTimeString();
                            $item->update_time   =   $item->updated_at->toDateTimeString();
                            unset($item->created_at);
                            unset($item->updated_at);
                        });

        if ($all_cat->isEmpty()) {
            return [true, []];
        }

        //分类无限极排序
        return [true, CategoryModel::generateTree($all_cat->toArray())];
    }

    /**
     * 添加分类
     * @param array $params  分类属性
     * @return array
     */
    public function catAdd(array $params)
    {
        $insert['cat_name'] =   $params['cat_name'];
        $insert['sorting']  =   $params['sorting'];
        $insert['unique_id']=   createUniqueID();
        $insert['cat_pic']  =   '';
        $insert['cat_icon'] =   '';
        $insert['ad_pic']   =   '';
        $insert['ad_url']   =   '';
        $insert['is_shelf'] =   CategoryModel::IS_SHELF;
        $insert['parent_id']=   $params['parent_id'];
        if ($insert['parent_id'] == 0) {
            $insert['level']    =   1;
        } else {
            $insert['level']    =   CategoryModel::where(['id' => $insert['parent_id']])->value('level') + 1;
        }

        $insert_result  =   CategoryModel::create($insert);
        if ($insert_result) {
            return [true, '添加成功'];
        } else {
            return [false, '添加失败'];
        }
    }

    /**
     * 删除分类
     * @param int $cat_id  分类ID
     * @return array
     */
    public function catDelete(int $cat_id)
    {
        if (CategoryModel::where(['parent_id' => $cat_id, 'is_delete' => CategoryModel::NOT_DELETE])->exists()) {
            return [false, '子分类存在,不允许删除'];
        }
        $del_result =   CategoryModel::where(['id' => $cat_id])->update(['is_delete' => CategoryModel::IS_DELETE, 'delete_time' => Carbon::now()->timestamp]);
        if ($del_result) {
            return [true, '删除成功'];
        } else {
            return [false, '删除失败'];
        }
    }

    /**
     * 下架分类
     * @param int $cat_id  分类ID
     * @param int $cat_status  分类操作
     * @return array
     */
    public function catOff(int $cat_id, int $cat_status)
    {
        if (CategoryModel::where(['parent_id' => $cat_id, 'is_delete' => CategoryModel::NOT_DELETE])->exists()) {
            return [false, '子分类存在,不允许下架'];
        }

        $off_result =   CategoryModel::where(['id' => $cat_id])->update(['is_shelf' => $cat_status]);
        if ($off_result) {
            return [true, '操作成功'];
        } else {
            return [false, '操作失败'];
        }
    }

}
